const path = require('path');
const HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin');
const isProduction = process.env.NODE_ENV === 'production';

const ApiLocalProxyList = {
  '/api/design': {
    target: 'http://localhost:6601',
    pathRewrite: {
      '^/api/design': ''
    },
    secure: false
  }
};

/**
 * 线上代理接口
 */
// http://121.199.54.250:81
const ApiOnlineProxyList = {
  '/zhzjmoocApi': {
    target: 'https://www.icve.com.cn/',
    changeOrigin: true,
    ws: true,
    pathRewrite: {
      '^/zhzjmoocApi': ''
    },
    secure: false
  },
  '/assets': {
    target: 'http://121.199.54.250:81',
    changeOrigin: true,
    ws: true,
    pathRewrite: {
      '^/api': ''
    },
    secure: false
  },
  
  '/api': {
    target: 'https://examtest.icve.com.cn/',
    changeOrigin: true,
    ws: true,
    pathRewrite: {
      '^/api': ''
    },
    secure: false
  },
  '/common': {
    target: 'https://zjy2.icve.com.cn/common',
    changeOrigin: true,
    ws: true,
    pathRewrite: {
      '^/common': ''
    },
    secure: false
  }
};

module.exports = {
  assetsDir: 'static',
  publicPath: '/exam/',
  chainWebpack: (config) => {
    // 修改入口文件
    config
      .entry('app')
      .clear()
      .add('babel-polyfill')
      .add('./src/app.ts');
    config.plugin('define').tap((args) => {
      let options = args[0];

      let rst = [
        {
          ...options,
          // 增加 环境变量 __DEV__
          __DEV__: !isProduction
        }
      ];
      return rst;
    });

    config.plugin('externals').use(HtmlWebpackExternalsPlugin, [
      {
        externals: [
          {
            module: 'moment',
            entry: 'min/moment-with-locales.min.js',
            global: 'moment'
          },
          {
            module: 'vue',
            entry: 'dist/vue.min.js',
            global: 'Vue'
          },
          {
            module: 'vuex',
            entry: 'dist/vuex.min.js',
            global: 'Vuex'
          },
          {
            module: 'vue-router',
            entry: 'dist/vue-router.min.js',
            global: 'VueRouter'
          },
          {
            module: 'axios',
            entry: 'dist/axios.min.js',
            global: 'axios'
          },
          {
            module: 'ant-design-vue',
            entry: 'dist/antd-with-locales.min.js',
            global: 'antd'
          },
          {
            type: 'css',
            module: 'ant-design-vue',
            entry: 'dist/antd.min.css'
          }
        ]
      }
    ]);
  },
  transpileDependencies: [
    'resize-detector',
    'element-resize-detector',
    'vue-to-pdf',
    'vue-easy-printer',
    'ant-design-vue'
  ],
  devServer: {
    host: '0.0.0.0',
    contentBase: [path.join(__dirname, 'assets')],
    port: 8080,
    proxy: process.env.online ? ApiLocalProxyList : ApiOnlineProxyList
  },
  productionSourceMap: false // 生产环境禁用map文件
};
